package com.ifonly.activerecord.sql.handlers;

import com.ifonly.activerecord.sql.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author <a href="mailto:ifonlymaster@163.com">ifonly</a>
 * @version 1.0 2015-12-25 16:38
 * @since JDK 1.6
 */
public abstract class AbstractMapKeyValueHandler<K, V> extends ResultSetHandler<Map<K, V>> {

    @Override
    public Map<K, V> handle(ResultSet rs) throws SQLException {
        Map<K, V> result = createMap();
        while (rs.next()) {
            result.put(createKey(rs), createValue(rs));
        }
        return result;
    }

    protected Map<K, V> createMap() {
        return new HashMap<K, V>();
    }

    protected abstract K createKey(ResultSet rs);

    protected abstract V createValue(ResultSet rs);

}
